package cn.engage.order.service.impl;

import cn.engage.basic.config.BussinessException;
import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.order.domain.OrderPetAcquisition;
import cn.engage.order.dto.OrderConfirmDto;
import cn.engage.order.mapper.OrderPetAcquisitionMapper;
import cn.engage.order.service.OrderPetAcquisitionService;
import cn.engage.pet.domain.Pet;
import cn.engage.pet.domain.PetDetail;
import cn.engage.pet.domain.SearchMasterMsg;
import cn.engage.pet.service.PetService;
import cn.engage.pet.service.SearchMasterMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author engage
 * @since 2022-11-01
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements OrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired
    private SearchMasterMsgService searchMasterMsgService;

    @Autowired
    private PetService petService;
    /**
     * 确认订单
     */
    @Override
    @Transactional
    public void confirm(OrderConfirmDto orderConfirmDto) {
        // 根据Id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findByIdOne(orderConfirmDto.getId());
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 查询寻主信息,修改状态为3
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findByIdOne(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgService.update(searchMasterMsg);
        // 保存宠物信息
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(orderConfirmDto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(orderPetAcquisition.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        pet.setDetail(new PetDetail());
        petService.add(pet);
        // 修改订单状态为完成
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(orderConfirmDto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }
    /**
     * 取消订单
     */
    @Override
    @Transactional
    public void cancel(Long orderId) {
        // 根据Id查询订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findByIdOne(orderId);
        if (orderPetAcquisition == null){
            throw new BussinessException("订单不存在！");
        }
        // 修改订单状态为取消
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 查询寻主信息
        SearchMasterMsg searchMasterMsg = searchMasterMsgService.findByIdOne(orderPetAcquisition.getSearchMasterMsgId());
        if (searchMasterMsg == null){
            throw new BussinessException("寻主消息不存在！");
        }
        // 修改状态,清空shopId
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgService.update(searchMasterMsg);
        // 保存取消日志
    }
}
